package divestoclimb.checklist;

import divestoclimb.checklist.data.Checklist;
import divestoclimb.checklist.storage.DbAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EditChecklist extends Activity implements Button.OnClickListener {

	protected long mId;
	protected long mCategoryId;

	private Button mCategoryButton;

	// The Checklist we are editing
	protected Checklist mChecklist;

	// The EditText field with the new text in it
	protected EditText mEditText;

	// This string is used to restore the previously entered text from a previous
	// state.
	protected String mSavedText;
	protected Long mSavedCategory;

	protected DbAdapter mDbHelper;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		Bundle params = icicle != null? icicle: getIntent().getExtras();

		mDbHelper = new DbAdapter(this);
		mDbHelper.open();

		mId = params.getLong(DbAdapter.KEY_CHECKLIST_ID);

		// Try to retrieve the text and category that we had before the activity was
		// stopped
		if(icicle != null) {
			mSavedText=icicle.getString(DbAdapter.KEY_ITEM_TEXT);
			mSavedCategory=icicle.getLong(DbAdapter.KEY_CHECKLIST_CATEGORY);
		} else {
			mSavedText = null;
			mSavedCategory = null;
		}

		setContentView(R.layout.name_category_dialog);

		mEditText = (EditText)findViewById(R.id.content);

		mCategoryButton = (Button)findViewById(R.id.cat_button);
		mCategoryButton.setOnClickListener(this);

		// Initialize the prompt
		TextView prompt = (TextView)findViewById(R.id.prompt);
		prompt.setText(getResources().getString(R.string.checklist_name_prompt));

		// Set up the button listener
		Button ok = (Button)findViewById(R.id.button_ok);
		ok.setOnClickListener(this);
		Button cancel = (Button)findViewById(R.id.button_cancel);
		cancel.setOnClickListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		// Look up the checklist we're editing
		mChecklist = mDbHelper.fetchChecklist(mId);

		// Populate the EditText with the existing name of this checklist, or
		// the saved text from the previous instance if the activity was stopped
		// and resumed
		mEditText.setText(mChecklist.getName());
		mCategoryId = mChecklist.getCategory().getID();
		if(mSavedText != null) {
			mEditText.setText(mSavedText);
		}
		if(mSavedCategory != null) {
			mCategoryId = mSavedCategory;
		}

		mCategoryButton.setText(mDbHelper.fetchCategory(mCategoryId).getName());
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putLong(DbAdapter.KEY_CHECKLIST_ID, mId);
		outState.putString(DbAdapter.KEY_ITEM_TEXT, mEditText.getText().toString());
		outState.putLong(DbAdapter.KEY_CHECKLIST_CATEGORY, mCategoryId);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(resultCode, requestCode, intent);
		if(requestCode == SelectCategory.CODE_CATEGORY_CHANGE && resultCode == RESULT_OK) {
			mSavedCategory = intent.getExtras().getLong(DbAdapter.KEY_CATEGORY_ID);
		}
	}

	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.button_cancel:
			setResult(RESULT_CANCELED);
			finish();
			break;
		case R.id.button_ok:
			// Update the name
			mChecklist.setName(mEditText.getText().toString());
			mChecklist.setCategory(mDbHelper.fetchCategory(mCategoryId));
			
			if(mChecklist.save(mDbHelper)) {
				setResult(RESULT_OK);
				finish();
			} else {
				Toast.makeText(this, R.string.checklist_save_failed, Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.cat_button:
			startActivityForResult(new Intent(this, SelectCategory.class), SelectCategory.CODE_CATEGORY_CHANGE);
		}
	}
}